package team.sun.sss.controller.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.TextUtils;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;
import team.sun.sss.pojo.staff.StaffTakeStudent;
import team.sun.sss.pojo.student.Family;
import team.sun.sss.pojo.student.StuFamily;
import team.sun.sss.pojo.user.User;

import javax.servlet.http.HttpSession;

@Controller
public class FamilyController {
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;

    @RequestMapping("/stu_family_update")
    public String toUpdateFamily(Model model, HttpSession session,@ModelAttribute(value = "stuFamily") StuFamily stuFamily) {
        User user = (User) session.getAttribute("loginInfo");
        familyDao1.updateFamily(stuFamily);
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("stuFamilyList", familyDao1.getFamilyList(user.getUserId()));
        return "student/student_update";
    }
    //删除家长信息
    @RequestMapping("/stu_family_delete/{num}")
    public String deleteFamily(@PathVariable("num") Integer num, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginInfo");
        familyDao1.deleteFamily(num);
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("stuFamilyList", familyDao1.getFamilyList(user.getUserId()));
        return "student/student_update";
    }
    @RequestMapping("/stu_family_add")
    public String deleteFamily( Model model, HttpSession session,
                                String familyRelation2, String familyName2, String familyPhone2, String familyWorkAddress2) {
        User user = (User) session.getAttribute("loginInfo");
       Family family=familyDao1.selectRelation(user.getUserId(),familyRelation2);
       if(family!=null){
           if(familyRelation2.equals(family.getFamilyRelation()))
               model.addAttribute("msg","该数据已存在");
        }else if(family==null){
            familyDao1.addFamily(user.getUserId(), familyRelation2, familyName2, familyPhone2, familyWorkAddress2);
    }
        model.addAttribute("msg","success");
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("stuFamilyList", familyDao1.getFamilyList(user.getUserId()));
        return "student/student_update";
    }
    //2.学工修改家长
    @PostMapping("/staff_family_update")
    public String staffFamilyUpdate(Model model, Integer stuId,
                                    String familyRelation, String familyName,String familyPhone,String familyWorkAddress,Integer familyNum) {
        familyDao1.staffUpdateFamily(familyRelation,familyName,familyPhone,familyWorkAddress,familyNum);
        model.addAttribute("studentInfo", studentDao.StuById(stuId));
        model.addAttribute("familyInfo", familyDao1.getFamilyList(stuId));
        return "redirect:/to_staff_stu_update/"+stuId;
    }
    @RequestMapping("/staff_family_delete/{num}/{stuId}")
    public String staffFamilyDelete(@PathVariable("num") Integer num,@PathVariable("stuId") Integer stuId,Model model) {
        //删除
        familyDao1.deleteFamily(num);
        model.addAttribute("studentInfo", studentDao.StuById(stuId));
        model.addAttribute("familyInfo", familyDao1.getFamilyList(stuId));

        return "redirect:/to_staff_stu_update/"+stuId;
    }
    //3.学生申请表
    @RequestMapping("/stuApply_family_update")
    public String stuApplyFamilyUpdate(Model model, HttpSession session,
                                 String submit, Integer familyNum,
                                 String familyRelation, String familyName, String familyWorkAddress,String familyWorkType,Float familyIncome
                                 ) {
        User user = (User) session.getAttribute("loginInfo");
        familyDao1.updateFamilyApply(familyRelation,familyName,familyWorkAddress,familyWorkType,familyIncome,familyNum);
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("familyList", familyDao1.getFamilyList(user.getUserId()));
        return "hardWork/hw_stu_applyWrite";
    }
    @RequestMapping("/stuApply_family_delete/{num}")
    public String stuApplyFamilyDelete(@PathVariable("num") Integer num, Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginInfo");
        //删除
        familyDao1.deleteFamily(num);
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        //获取家长信息
        model.addAttribute("familyList", familyDao1.getFamilyList(user.getUserId()));
        return "hardWork/hw_stu_applyWrite";
    }
    @RequestMapping("/stuApply_family_add")
    public String stuApplyFamilyAdd( Model model, HttpSession session,
                                     String familyRelation2, String familyName2, String familyWorkAddress2,String familyWorkType2,
                                     Float familyIncome2) {
        User user = (User) session.getAttribute("loginInfo");
        Family family=familyDao1.selectRelation(user.getUserId(),familyRelation2);
        if(family!=null){
            if(familyRelation2.equals(family.getFamilyRelation()))
                model.addAttribute("msg","该数据已存在");
        }else if(family==null){
            familyDao1.addApplyFamily(user.getUserId(),familyRelation2,familyName2,familyWorkAddress2,familyWorkType2,familyIncome2);
        }
        model.addAttribute("msg","success");
        model.addAttribute("studentList", studentDao.selectStuById(user.getUserId()));
        model.addAttribute("familyList", familyDao1.getFamilyList(user.getUserId()));
        return "hardWork/hw_stu_applyWrite";
    }
}
